package com.coin.service;

import com.coin.common.BusinessType;
import com.coin.common.FlowDirect;
import com.coin.dao.account.AccountMapper;
import com.coin.dao.flow.TradeFlowMapper;
import com.coin.dao.model.TradeFlow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by chenhao on 2017/5/18.
 */
@Service
public class BalanceTransferService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private TradeFlowMapper tradeFlowMapper;

    @Transactional(value = "accountTransactionManager")
    public Long balanceTransfer(BigDecimal amount, Long outMemberId, Long inMemberId, Long businessId, Integer businessType) throws Exception {
        if (BigDecimal.ZERO.compareTo(amount) > 0) return -1L;
        long flowId = -1;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
        FlowDirect direct = BusinessType.findFlowDirectByBusinessType(businessType);
        int subtractEffect = accountMapper.subtractBalanceWithoutZeroLimit(amount, outMemberId, direct.getOutMemberType(), direct.getOutAccountType(), String.valueOf(outMemberId % 16));
        if (subtractEffect == 1) {
            int addEffect = accountMapper.addBalance(amount, inMemberId, direct.getInMemberType(), direct.getInAcountType(), String.valueOf(inMemberId % 16));
            if (addEffect == 1) {
                TradeFlow tradeFlow = new TradeFlow();
                tradeFlow.setAmount(amount);
                tradeFlow.setBusinessType(businessType);
                tradeFlow.setBusinessId(businessId);
                tradeFlow.setOutMemberId(outMemberId);
                tradeFlow.setOutMemberType(direct.getOutMemberType());
                tradeFlow.setOutMemberAccount(direct.getOutAccountType());
                tradeFlow.setInMemberId(inMemberId);
                tradeFlow.setInMemberType(direct.getInMemberType());
                tradeFlow.setInMemberAccount(direct.getInAcountType());
                tradeFlow.setTableId(simpleDateFormat.format(new Date()));
                tradeFlowMapper.insert(tradeFlow);//失败则accountTransactionManager 回滚account库，成功则完成整个交易
                flowId = Long.valueOf(tradeFlow.getTableId() + "" + tradeFlow.getId());
            } else {
                throw new Exception("accountMapper.addBalance Exception");
            }
        } else {
            throw new Exception("accountMapper.subtractBalance Exception");
        }
        return flowId;
    }
}
